package com.school.dao.impl;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.school.dao.CourseDao;
import com.school.domain.Course;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class CourseDaoImpl implements CourseDao {
    private DataSource dataSource = new ComboPooledDataSource();
    private QueryRunner queryRunner = new QueryRunner(dataSource);

    @Override
    public boolean add(Course course) {
        int result = 0;
        try {
            result = queryRunner.update("insert into course values(?, ?, ?, ?, ?, ?, ?)", null, course.getName(), course.getNum(), 0, course.getRid(), course.getTbid(), course.getTid());

        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }

    @Override
    public boolean del(long coid) {
        int result = 0;
        try {
            result = queryRunner.update("delete from course where coid = ?", coid);
        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }

    @Override
    public boolean del(String name) {
        int result = 0;
        try {
            result = queryRunner.update("delete from course where name = ?", name);
        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }

    @Override
    public boolean update(Course course) {
        int result = 0;
        try {
            result = queryRunner.update("update course set name = ?, num = ?, nowNum = ?, rid = ?, tbid = ?, tid = ? where coid = ?", course.getName(), course.getNum(), course.getNowNum(), course.getRid(), course.getTbid(), course.getTid(), course.getCoid());
        } catch (SQLException throwables) {
            return false;
        }
        return result == 1;
    }


    @Override
    public Course findById(long coid) {
        List<Course> execute = null;
        try {
            execute = queryRunner.query("select * from course where coid = " + coid, new BeanListHandler<Course>(Course.class));
        } catch (SQLException throwables) {
            return null;
        }
        if (execute.size() < 1) return null;
        return execute.get(0);
    }

    @Override
    public Course findByName(String name) {
        List<Course> execute = null;
        try {
            execute = queryRunner.query("select * from course where name = " + name, new BeanListHandler<Course>(Course.class));
        } catch (SQLException throwables) {
            return null;
        }
        if (execute.size() < 1) return null;
        return execute.get(0);
    }

    @Override
    public List<Course> findByTid(long tid) {
        List<Course> execute = null;
        try {
            execute = queryRunner.query("select * from course where tid = " + tid, new BeanListHandler<Course>(Course.class));
        } catch (SQLException throwables) {
            return null;
        }
        return execute;
    }

    @Override
    public List<Course> findAll() {
        List<Course> execute = null;
        try {
            execute = queryRunner.query("select * from course", new BeanListHandler<Course>(Course.class));
        } catch (SQLException throwables) {
            return null;
        }
        return execute;
    }
}
